import { server } from '../config/server';

export const user = {
    getWxLoginCode() {
        return new Promise((resolve, reject) => {
            wx.login({
                success: (res) => {
                    console.log('【 Response in request getWxLoginCode 】', res);
                    resolve({success: true, code: res.code});
                },
                fail: (err) => {
                    console.log('【 Error in request getWxLoginCode 】', err);
                    reject({success: false, msg: "请检查网络连接"});
                }
            });
        });
    },
    /*
     * 小程序登录功能 并 保存Token到本地存储
     */
    async wxLoginAndSaveToken() {
        return new Promise((resolve, reject) => {
            this.getWxLoginCode().then((res) => {
                wx.request({
                    // 需要替换为真实的接口地址
                    url: `${server.server_addr}/user/wxLoginBycode`,
                    method: 'POST',
                    data: { code: res.code },
                    success: (res) => {
                        // 存储token
                        wx.setStorageSync('loginToken', res.data.data.loginToken);
                        wx.setStorageSync('powerToken', res.data.data.powerToken);
                        wx.setStorageSync('uid', res.data.data.uid);
                        console.log('【 Response in request wxLoginAndSaveToken 】', res);
                        resolve(res.data);
                    },
                    fail: (err) => {
                        console.log('【 Error in request wxLoginAndSaveToken 】', err);
                        reject({ success: false, err });
                    }
                });
            }).catch((err) => {
                wx.showToast({
                    title: err.msg,
                });
            });
        });
    },
    /*
     * 检查用户是否具有登录态
     */
    checkWxLoginStatus(loginToken, powerToken) {
        return new Promise((resolve, reject) => {
            wx.request({
                // 需要替换为真实的接口地址
                url: `${server.server_addr}/user/checkUserLoginByJwt`,
                method: 'POST',
                data: { loginToken, powerToken },
                success: (res) => {
                    console.log('【 Response in request checkUserLoginByJwt 】', res);
					resolve(res.data);
                },
                fail: (err) => {
                    console.log('【 Error in request checkUserLoginByJwt 】', err);
					reject({ success: false, err });
                }
            });
        });
    },
    // 获取用户信息
    getUserInfoByJwt() {
        return new Promise((resolve, reject) => {
            let loginToken = wx.getStorageSync('loginToken');
            let powerToken = wx.getStorageSync('powerToken');
            if (!loginToken || !powerToken) {
                reject({ success: false, err: new Error("本地缺少token") });
                return ;
            }
            wx.request({
                // 需要替换为真实的接口地址
                url: `${server.server_addr}/user/getUserInfoByJwt`,
                method: 'POST',
                data: { loginToken, powerToken },
                success: (res) => {
                    console.log('【 Response in request checkUserLoginByJwt 】', res);
					resolve(res.data);
                },
                fail: (err) => {
                    console.log('【 Error in request checkUserLoginByJwt 】', err);
					reject({ success: false, err });
                }
            });
        });
    }
    
};